<template>
  <div class="container">
    <h2 class="title">{{$t('m.container.info')}}</h2>
    <el-form ref="refForm" :model="formData" :rules="rules">
      <el-form-item :label="$t('m.container.name')" prop="strategyId" label-width="100px">
        <el-select v-model="formData.strategyId" filterable placeholder @change="getInfo">
          <el-option v-for="(item,i) in strategyList" :key="i" :label="item.strategyName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-table :data="formData.groupList">
        <el-table-column align="center" type="index" width="100"></el-table-column>
        <el-table-column align="center" :label="$t('m.page.region')">
          <template slot-scope="scope">
            <el-form-item :prop="'groupList.'+scope.$index+'.areaIdList'" :rules="rules.areaIdList">
              <el-select disabled v-model="scope.row.areaIdList" multiple filterable placeholder>
                <el-option-group v-for="(group,index) in partitionList" :key="group.partitionname+index" :label="group.partitionname">
                  <el-option style="padding-left:50px" v-for="(item,i) in group.warehouseareas" :key="i" :label="item.areaname" :value="item.areaid"></el-option>
                </el-option-group>
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('m.container.user')">
          <template slot-scope="scope">
            <el-form-item :prop="'groupList.'+scope.$index+'.areaUserIdList'">
              <el-select v-model="scope.row.areaUserIdList" multiple filterable placeholder>
                <el-option v-for="(item,i) in userList" :key="i" :label="item.username" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" :label="$t('m.container.truck')">
          <template slot-scope="scope">
            <el-form-item :prop="'groupList.'+scope.$index+'.truckUserIdList'">
              <el-select v-model="scope.row.truckUserIdList" multiple filterable placeholder>
                <el-option v-for="(item,i) in userList" :key="i" :label="item.username" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
      </el-table>
      <div class="btns">
        <el-button type="primary" @click="save">{{$t('m.save')}}</el-button>
        <el-button @click="back">{{$t('m.cancel')}}</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name:'',
  data(){
    return {
      formData:{
        strategyId:null
      },
      strategyList:[],
      partitionList:[],
      userList:[]
    }
  },
  props:['obj'],
  computed:{
    rules(){
      return {
        strategyId:[
          {
            required:true,
            message:this.$t('m.choose') + this.$t('m.container.name'),
            trigger:'change'
          }
        ],
        areaUserIdList:[
          {
            required:true,
            message:this.$t('m.choose') + this.$t('m.container.user'),
            trigger:'change'
          }
        ],
        truckUserIdList:[
          {
            required:true,
            message:this.$t('m.choose') + this.$t('m.container.truck'),
            trigger:'change'
          }
        ]
      }
    }
  },
  watch:{
  },
  methods:{
    getInfo(strategyId){
      if (strategyId){
        this.$axios({
          method:'get',
          url:`/schedulingStrategy/group/${strategyId}`
        }).then(({ data }) => {
          this.formData = data
          this.formData.groupList.forEach(element => {
            this.$set(element, 'areaIdList', element.areas.map(v => {
              return v.areaid
            }))
          })
          this.$nextTick(() => {
            this.$refs.refForm.clearValidate()
          })
        })
      }
    },
    getUser(){
      this.$axios({
        method:'get',
        url:`/user/list`
      }).then(({ data }) => {
        this.userList = data
      })
    },
    getPartition(){
      return new Promise((resolve) => {
        this.$axios({
          method:'get',
          url:`/areascheduing/partition/list`
        }).then(({ data }) => {
          this.partitionList = data.filter((v) => {
            return v.warehouseareas.length > 0
          })
          this.partitionList.forEach(element => {
            element.warehouseareas.forEach(e => {
              this.$set(e, 'isdisabled', false)
            })
          })
          let partitionid = this.partitionList[0].partitionid
          let areaid = this.partitionList[0].warehouseareas[0].areaid
          resolve({ partitionid, areaid })
        })
      })
    },
    setStrategy(){
      this.$axios({
        method:'get',
        url:'/schedulingStrategy/get'
      }).then(({ data }) => {
        this.strategyList = data
      })
    },
    save(){
      this.$refs['refForm'].validate(valid => {
        if (valid){
          this.$axios({
            method:'post',
            url:`/areascheduing/update/strategy`,
            data:this.formData.groupList
          }).then(() => {
            this.$message.success(this.$t('m.quality.success'))
            this.back()
          })
        }
      })
    },
    back(){
      let obj = {
        componentName:'scheduling'
      }
      this.$emit('switchComponent', obj)
    }
  },
  activated(){
    this.formData = {
      strategyName:'',
      groupList:[]
    }
    this.$nextTick(() => {
      this.$refs.refForm.clearValidate()
    })
    this.setStrategy()
    this.getUser()
    this.getPartition()
  }
}
</script>

<style lang='scss' scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
}
.title {
  margin-bottom: 30px;
}

::v-deep.el-table .el-form-item{
  margin-bottom: 0;
}
</style>
